<template>
	<u-modal v-model="show" :show-cancel-button="true" confirm-text="升级" title="发现新版本" @cancel="cancel" @confirm="confirm">
		<view class="u-update-content">
			<!-- <rich-text :nodes="content"></rich-text> -->
			{{content}}
		</view>
	</u-modal>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				content:'',
				url: '',
			}
		},
		onReady() {
			this.show = true;
		},
		onLoad(e) {
			this.url = decodeURIComponent(e.url)
			this.content = this.api.uploadMsg
		},
		methods: {
			cancel() {
				this.closeModal();
			},
			confirm() {
				// this.closeModal();
				this.uploadApk()
			},
			uploadApk() {
				let appVersion = ''
				uni.getSystemInfo({
					success: function(e) {
						appVersion = e.platform
					}
				})
				// uni.showLoading({
				// 	title: '更新中……'
				// })
				// 安卓直接下载
				if (appVersion === 'android') {
					var dtask = plus.downloader.createDownload(this.url, {}, function(d, status) {
						uni.showToast({
							title: '下载完成',
							mask: false,
							duration: 1000
						});
						// 下载完成
						if (status == 200) {
							plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, e => e, function(error) {
								uni.showToast({
									title: '安装失败-01',
									mask: false,
									duration: 1500
								});
							});
						} else {
							uni.showToast({
								title: '更新失败-02',
								mask: false,
								duration: 1500
							});
						}
					});
					try {
						dtask.start(); // 开启下载的任务
						var prg = 0;
						var showLoading = plus.nativeUI.showWaiting('正在下载'); //创建一个showWaiting对象
						dtask.addEventListener('statechanged', function(task, status) {
							// 给下载任务设置一个监听 并根据状态  做操作
							switch (task.state) {
								case 1:
									showLoading.setTitle('正在下载');
									break;
								case 2:
									showLoading.setTitle('已连接到服务器');
									break;
								case 3:
									prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
									showLoading.setTitle('  正在下载' + prg + '%  ');
									break;
								case 4:
									plus.nativeUI.closeWaiting();
									//下载完成
									break;
							}
						});
					} catch (err) {
						plus.nativeUI.closeWaiting();
						uni.showToast({
							title: '更新失败-03',
							mask: false,
							duration: 1500
						});
					}
					//如果是ios跳转到app store
				} else {
					//在App Store Connect中的App Store下的app信息，可找到appleId
					let appleId = 1535881389;
					plus.runtime.launchApplication({
						action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
					}, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					});
					uni.reLaunch({
						url:'/uview-ui/components/u-full-screen/u-full-screen?url='+encodeURIComponent(this.url)
					})
				}

			},
			closeModal() {
				// uni.navigateBack();
				if(!uni.getStorageSync('store_id')){
					// let set =setTimeout(function(){
						uni.reLaunch({
							url:"/pages/user/logIn"
						})
						// clearTimeout(set)
					// },2000)
				}else{
					// let set = setTimeout(function(){
						uni.switchTab ({
							url:"/pages/index/index"
						})
						// clearTimeout(set)
					// },2000)
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	@import "../../libs/css/style.components.scss";

	.u-full-content {
		background-color: #00C777;
	}

	.u-update-content {
		font-size: 26rpx;
		color: $u-content-color;
		line-height: 1.7;
		padding: 30rpx;
		white-space: pre-wrap;
	}
</style>
